{
 "cells": [
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "<!--BOOK_INFORMATION-->\n",
    "<img align=\"left\" style=\"padding-right:10px;\" src=\"figures/PHydro-cover-small.png\">\n",
    "*This is the Jupyter notebook version of the [Python in Hydrology](http://www.greenteapress.com/pythonhydro/pythonhydro.html) by Sat Kumar Tomer.*\n",
    "*Source code is available at [code.google.com](https://code.google.com/archive/p/python-in-hydrology/source).*\n",
    "\n",
    "*The book is available under the [GNU Free Documentation License](http://www.gnu.org/copyleft/fdl.html). If you have comments, corrections or suggestions, please send email to satkumartomer@gmail.com.*"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "<!--NAVIGATION-->\n",
    "< [7. Plotting](07.00-Plotting.ipynb) | [Contents](Index.ipynb) | [Bar Charts](07.02-Bar-Charts.ipynb) >"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## 7.1 时间轴\n",
    "\n",
    "大部分水文数据是时间序列数据。要绘制这些数据，要求x轴应该是时间轴，而y轴应该绘制数据。我们将使用`scikits`包中的`timeseries`库(译者注：在python3.5中不可用，可采用pandas处理时间序列数据)来处理时间序列数据，同时也提供了绘制时间序列的功能。我们将在固定(每日)间隔生成一些数据，就像使用某些模型来模拟数据，以及一些随机时间的数据，就像在不规则的时间间隔进行测量一样。测量数据还包含相应的时间向量，即年、月和日。"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 1,
   "metadata": {},
   "outputs": [
    {
     "ename": "ImportError",
     "evalue": "No module named 'scikits'",
     "output_type": "error",
     "traceback": [
      "\u001b[1;31m---------------------------------------------------------------------------\u001b[0m",
      "\u001b[1;31mImportError\u001b[0m                               Traceback (most recent call last)",
      "\u001b[1;32m<ipython-input-1-7fe9e00c3afe>\u001b[0m in \u001b[0;36m<module>\u001b[1;34m()\u001b[0m\n\u001b[0;32m      1\u001b[0m \u001b[1;31m#import pandas as pd\u001b[0m\u001b[1;33m\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0;32m      2\u001b[0m \u001b[1;32mimport\u001b[0m \u001b[0mnumpy\u001b[0m \u001b[1;32mas\u001b[0m \u001b[0mnp\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[1;32m----> 3\u001b[1;33m \u001b[1;32mimport\u001b[0m \u001b[0mscikits\u001b[0m\u001b[1;33m.\u001b[0m\u001b[0mtimeseries\u001b[0m \u001b[1;32mas\u001b[0m \u001b[0mts\u001b[0m \u001b[1;31m#停止更新，不再支持\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0m\u001b[0;32m      4\u001b[0m \u001b[0mx\u001b[0m \u001b[1;33m=\u001b[0m \u001b[0mnp\u001b[0m\u001b[1;33m.\u001b[0m\u001b[0marange\u001b[0m\u001b[1;33m(\u001b[0m\u001b[1;36m500\u001b[0m\u001b[1;33m)\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0;32m      5\u001b[0m \u001b[0my_sim\u001b[0m \u001b[1;33m=\u001b[0m \u001b[0mnp\u001b[0m\u001b[1;33m.\u001b[0m\u001b[0msin\u001b[0m\u001b[1;33m(\u001b[0m\u001b[0mx\u001b[0m\u001b[1;33m/\u001b[0m\u001b[1;36m25.0\u001b[0m\u001b[1;33m)\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n",
      "\u001b[1;31mImportError\u001b[0m: No module named 'scikits'"
     ]
    }
   ],
   "source": [
    "#import pandas as pd\n",
    "import numpy as np\n",
    "import scikits.timeseries as ts #停止更新，不再支持\n",
    "x = np.arange(500)\n",
    "y_sim = np.sin(x/25.0)\n",
    "year = [2009, 2010, 2010, 2010, 2011]  # year for measurement\n",
    "month = [10, 2, 5, 9, 1]   # month for measurement\n",
    "day = [20, 15, 17, 22, 15]  # day for measurement\n",
    "y_meas = [0.4, -0.5, 0, 1, 0]"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "我们从创建规则数据的时间序列开始。为此，首先我们定义开始日期，然后使用开始日期和模拟数据来使用`ts.timeseries`创建时间序列对象。"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "first_date = ts.Date(freq='D',year=2009,month=10,day=5)\n",
    "data_series = ts.time_series(y_sim, start_date=first_date)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "由于测量的数据不是固定的频率，所以我们不能如此容易地定义它。我们需要为每个日期创建日期对象，然后创建时间序列对象。"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "date = []\n",
    "for i in range(len(year)):\n",
    "    date.append(datetime.date(year[i], month[i], day[i]))\n",
    "meas_series = ts.time_series(y_meas, dates=date,freq='D')"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "`scikits.timeseries`提供`lib.plotlib`来绘制时间序列图。这个库具有类似的功能来作图，因此很容易采用。图7.1显示了具有时间轴的图。\n",
    "\n",
    "![timeseries](figures/date.png)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "<center>图7.1：显示x轴日期的图</center>"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.5.4"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 2
}
